package com.nine.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nine.common.pojo.Role;
import com.nine.common.vo.Result;
import com.nine.system.service.RoleService;
import com.nine.system.vo.RoleVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: nine-education-parent
 * @author: ♥丁新华
 * @create: 2023-05-25 10:15
 **/
@RestController
@RequestMapping("system/role")
public class RoleController {


    /**
     * 查 询 角 色 ID
     */

    //注入role
    @Autowired
    private RoleService roleService;

    @GetMapping("/findByUserID/{userId}")
    @ApiOperation (value = "查询角色")
    public Result findByUserID(@PathVariable String userId){

        List<Role> all = roleService.findAll();

        List<Role> roles = roleService.findByUserID (userId);

        List<String> roleids = roles.stream().map(item -> item.getId()).collect(Collectors.toList());

        Map<String,Object> map=new HashMap<>();
        map.put("all",all);
        map.put("roles",roleids);


        return Result.success(map);

    }

    /**
     * 角 色 修 改 确 认
     */

    @PostMapping("confirm")
    @ApiOperation (value = "确认修改角色")
    public Result confirm(String userId, String[] roleIds){

        Integer row = roleService.confirm(userId,roleIds);

        return Result.success();

    }

    /**
     * 分页查询角色
     */

    @PostMapping("/list/{current}/{pageSize}")
    @ApiOperation (value = "分页查询角色列表")
    public Result listRole(@PathVariable Integer current, @PathVariable Integer pageSize, @RequestBody RoleVo roleVo){
        Page<Role> aclRolePage = roleService.roleList (current, pageSize, roleVo);

        return Result.success (aclRolePage);
    }

}
